package com.ming.springboot.data;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;

import javax.swing.*;

/**
 * Created by mqsi on 2022/5/15 17:20
 */
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Data
public class ErrorResult {
	private Integer resultCode;
	private String message;
	private String exception;

	// 通过builder方式创建实体
	public static Builder builder(){
		return new Builder();
	}

	public static ErrorResult fail(ResultCode resultCode, Throwable e){
		return builder().message(e.getMessage())
				.resultCode(resultCode.getCode())
				.exception(e.getClass().getName())
				.build();
	}

	public static ErrorResult fail(ResultCode resultCode, Throwable e, String message){
		ErrorResult result = ErrorResult.fail(resultCode, e);
		result.setMessage(message);
		return result;
	}

	@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
	public static class Builder {
		private Integer code;
		private String message;
		private String exception;
		public Builder resultCode(Integer resultCode){
			this.code = resultCode;
			return this;
		}
		public Builder message(String message){
			this.message = message;
			return this;
		}
		public Builder exception(String exception){
			this.exception = exception;
			return this;
		}
		public final ErrorResult build(){
			return new ErrorResult(code, message, exception);
		}
	}

}
